from src.api.https import SUCCESS
from src.model.TypeModel import TypeModel
from src.init.init_db import db

class TypeTool:

    # 预置标签数据
    def preadd(self):
        self.init("文章", None)
        self.init("公告", None)
        self.init("任务", None)
        self.init("已经被领取的任务", 3)

    def __init__(self):
        self.typename = None
        self.mod = 0
        self.url = None


    def init(self, typename:str, fatherid:int) -> None:
        self.typename = typename
        self.fatherid = fatherid
        type = TypeModel(name=typename, fid=fatherid)
        db.session.add(type)
        db.session.commit()


    def check(self, typename:str, fatherid:int):
        if len(typename) == 0:
            return "标签名为空！"
        elif len(typename) > 100:
            return "标签名太长，长度在100以内！"
        elif TypeModel.query.filter_by(name=typename).first() is not None:
            return "标签名已存在！"
        elif fatherid is None:
            return SUCCESS
        else:
            if TypeModel.query.filter_by(tid=fatherid).first() is None:
                return "指定父标签不存在！"
        return SUCCESS

    def delete(self, tid:int):
        if TypeModel.query.filter_by(tid=tid).first() is None:
            return "要删除的标签不存在！"
        db.session.delete(TypeModel.query.filter_by(tid=tid).first())
        db.session.commit()
        return SUCCESS

    def list(self):
        data = TypeModel.query.all()
        for i in range(len(data)):
            data[i] = data[i].to_dict()
        return data

    def update(self, id:int, name:str, fid:int):
        TypeModel.query.filter_by(tid=id).update(dict(name=name, fid=fid))
        db.session.commit()
        return SUCCESS

    def get(self, tid:int):
        if TypeModel.query.filter_by(tid=tid).first() is None:
            return False
        return TypeModel.query.filter_by(tid=tid).first().to_dict()

    def exists(self, tid:int):
        if TypeModel.query.filter_by(tid=tid).first() is None:
            return False
        return True

    def list_article_tags(self):
        data = []
        article_tag = TypeModel.query.filter_by(name="文章").first()
        data.append(article_tag)
        data += self.recursive_query(article_tag)
        res = []
        for i in data:
            res.append(i.tid)
        return res

    def list_announce_tags(self):
        data = []
        announce_tag = TypeModel.query.filter_by(name="公告").first()
        data.append(announce_tag)
        data += self.recursive_query(announce_tag)
        res = []
        for i in data:
            res.append(i.tid)
        return res

    def list_task_tags(self):
        data = []
        task_tag = TypeModel.query.filter_by(name="任务").first()
        data.append(task_tag)
        data += self.recursive_query(task_tag)
        res = []
        for i in data:
            res.append(i.tid)
        return res


    #递归查询子标签
    def recursive_query(self, tag:TypeModel):
        res = TypeModel.query.filter_by(fid=tag.tid).all()
        data = []
        for i in res:
            data += self.recursive_query(i)
        res += data
        return res